package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDish;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
@Autowired
private CategoryService categoryService;
    @PostMapping
    @CacheEvict(value = "setmealcache",allEntries = true)//清除setmealcache名称下，所有的缓存数据
    @ApiOperation(value = "新增套餐接口")
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息：{}", setmealDto);
        //调用setmealservice中的saveDish方法，
        setmealService.saveDish(setmealDto);
        return R.success("新增套餐成功");
    }
@GetMapping("/page")
@ApiOperation(value = "套餐分页查询接口")
@ApiImplicitParams({
        @ApiImplicitParam(name = "page",value = "页码",required = true),
        @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
        @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
})
    public R<Page> page(int page,int pageSize,String name){
        //创建分页构造器对象
    Page<Setmeal> pageinfo = new Page<>(page,pageSize);
    Page<SetmealDto> dtoPage = new Page<>();
    //创建一个条件构造器
    LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
    //添加查询条件，根据name进行like模糊查询
    queryWrapper.like(name!=null,Setmeal::getName,name);
    //添加排序条件，根据更新时间降序排序
    queryWrapper.orderByDesc(Setmeal::getCreateTime);
    setmealService.page(pageinfo,queryWrapper);
    //records在Page里面，代表的是记录，
    // 因为两个泛型不一样，所以要将它排除在拷贝外
    BeanUtils.copyProperties(pageinfo,dtoPage,"records");
//如果要忽略掉records，所以要自己设置records的值
    //先获取到records的值
    List<Setmeal> records = pageinfo.getRecords();
    //因为records的类型是<Setmeal>，
    // 要想将泛型转换成<SetmealDto>,就需要计算
    //此处的item代表的是Setmeal类型的对象
    //将这些数据赋值给SetmealDto对象
    List<SetmealDto> list=records.stream().map((item)->{
        //创建SetmealDto对象，因为是自己创建的对象，所以里面是空
        SetmealDto setmealDto = new SetmealDto();
        //将item对象的数据拷贝到SetmealDto中
        BeanUtils.copyProperties(item,setmealDto);
        //item代表的就是Setmeal的数据
        //获取到CategoryId
        Long categoryId = item.getCategoryId();
        //根据id查询分类对象
        Category byId = categoryService.getById(categoryId);
        //判断分类对象byid是否为空
        if (byId!=null){
            //如果不为空，获取分类名称
            String name1 = byId.getName();
            //通过SetmealDto，修改name1的属性
            //因为又新创建的对象，所以setmealDto中是空的
            //现在只是将name1赋值进去而已，还没有其他的属性和方法
            setmealDto.setCategoryName(name1);
        }
        //将SetmealDto对象返回
return setmealDto;
    }).collect(Collectors.toList());
    //将更改后的records再传回Pagedto，PageDto中的值就齐全了
    dtoPage.setRecords(list);
    //然后将Pagedto返回响应数据
    return R.success(dtoPage);
}

//删除套餐
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//清除setmealcache名称下所有的缓存数据
    @ApiOperation(value = "套餐删除接口")
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.deletes(ids);
        return R.success("删除套餐成功");
    }

    //根据条件查询套餐
    @GetMapping("/list")
    @Cacheable(value = "setmeal",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @ApiOperation(value = "套餐条件查询接口")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        if (setmeal.getCategoryId()!=null){
            queryWrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
            queryWrapper.eq(Setmeal::getStatus,setmeal.getStatus());
            queryWrapper.orderByDesc(Setmeal::getUpdateTime);
            return R.success(setmealService.list(queryWrapper));
        }
        return R.error("没有套餐");
    }
/*    *//*
    * 根据条件查询套餐数据
    * *//*
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper();
lambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
lambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(lambdaQueryWrapper);
        return R.success(list);
    }*/
@PostMapping("/status/{status}")
public R<String> statuss(@PathVariable Integer status,@RequestParam  List<Long> ids){
    log.info("停售：{}",ids);
    LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
    updateWrapper.in(Setmeal::getId, ids);
    updateWrapper.set(Setmeal::getStatus,status);
    setmealService.update(updateWrapper);
    return R.success("");
}
}
